package my.algo.util;

import java.util.*;

/**
 * Java语言层面的一些工具类的使用技巧
 * @author fengbo
 * @date 2018/5/16
 */
public class MyUtils {

    private static List<String> arrayToList() {
        String[] array = {"b", "a", "c"};
        return new ArrayList<>(Arrays.asList(array));
    }

    private static String[] listToArray() {
        List<String> list = new ArrayList<>(3);
        Collections.addAll(list, "a", "b", "c");
        String[] array = new String[3];
        return list.toArray(array);
    }

    private static String[] arrayCopy() {
        String[] strings = listToArray();
        String[] result = new String[3];
        // 等同于 (Arrays.copyOf(strings, strings.length);)
        System.arraycopy(strings, 0, result, 0, strings.length);
        return result;
    }

    public static void main(String[] args) {
        String[] strings = arrayCopy();
        System.out.println(Arrays.toString(strings));

        // 集合操作
        List<String> list = arrayToList();
        list.add("d");
        System.out.println(list);
        // 排序
        Collections.sort(list);
        System.out.println(list);
        // 搜索
        int i = Collections.binarySearch(list, "a");
        System.out.println(i);
        // 反转  --  反转后使用搜索会出现问题
        Collections.reverse(list);
        System.out.println(list);
        // 交换
        Collections.swap(list, 0, 2);
        System.out.println(list);
    }
}
